package com.entities.projectversion.servicelayer;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.core.hibernate.ExceptionHandler;
import com.core.hibernate.exceptionevent.ExceptionEvent;
import com.core.managers.SessionFactoryManager;
import com.core.protocols.IHibernateParentEnumeratorDelegate;
import com.core.protocols.IKVObject;
import com.core.protocols.IServiceLayerDelegate;
import com.core.servicelayer.HibernateServiceLayer;
import com.entities.projectitem.servicelayer.ProjectListLayer;
import com.model.project.ProjectItem;
import com.model.project.Version;


public class VersionListLayer
        extends HibernateServiceLayer {

	public Class <? extends IKVObject> entityClass () {

		return Version.class;
	}



	public void enumerateParentObjectsWithDelegate ( IKVObject kvObject,
	                                                 IHibernateParentEnumeratorDelegate delegate ) {

		if ( kvObject == null ) {
			return;
		}
		Version projectVersion = (Version) kvObject;
		delegate.onEnumarateParent(projectVersion);

		ProjectItem projectItem = projectVersion.getParentProject();
		delegate.onEnumarateParent(projectItem);
	}



	public IKVObject addProjectVersion ( IKVObject template,
	                                     Integer parentProjectId ) {


		IKVObject result = null;

		/**
		 * check 4 template
		 */

		if ( template == null ) {
			return result;
		}


		IServiceLayerDelegate delegate = this.getLayerDelegate();


		Session session = SessionFactoryManager.sharedInstance().openSession();
		/**
		 * check 4 session
		 */
		if ( session == null ) {
			return result;
		}

		ProjectListLayer projectListLayer = new ProjectListLayer();
		ProjectItem projectItem = (ProjectItem) projectListLayer.objectByUNID(parentProjectId, session, null);

		/**
		 * check 4 parent item
		 */
		if ( projectItem == null ) {
			if ( delegate != null ) {
				delegate.responseResponseItemsList(null, this, new Exception("Project not found"));
			}
			return null;
		}



		/**
		 * create an entity
		 */

		Exception exception = null;
		try {

			result = VersionListLayer.createEntityWithTemplate(template, null, session);

		} catch ( Exception e ) {
			result = null;
		}


		if ( result == null ) {
			return result;
		}

		/**
		 * add to parent list
		 */


		Transaction transaction = null;
		try {

			Version version = (Version) result;
			transaction = session.beginTransaction();
			version.setParentProject(projectItem);
			projectItem.getProjectVersions().add(version);
			transaction.commit();

		} catch ( Exception e1 ) {
			ExceptionHandler.handleException(e1, new ExceptionEvent(ExceptionEvent.ADDCHILDOBJECT));
			result = null;
		}

		List <IKVObject> list = null;
		if ( result != null ) {
			list = new ArrayList <IKVObject>();
			list.add(result);
		}

		/**
		 * send result to delegate
		 */

		if ( delegate != null ) {
			delegate.responseResponseItemsList(list, this, exception);
		}



		return result;
	}
}
